from extensions import db
from flask_login import UserMixin

# 定义角色常量
class UserRole:
    ADMIN = 'admin'
    EDITOR = 'editor'
    USER = 'user'

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    role = db.Column(db.String(20), default=UserRole.USER)  # 新增角色字段

    def is_admin(self):
        return self.role == UserRole.ADMIN

    def is_editor(self):
        return self.role == UserRole.EDITOR

    def is_user(self):
        return self.role == UserRole.USER

class Penalty(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    site = db.Column(db.SmallInteger, nullable=True)
    url = db.Column(db.String(64), nullable=True, comment='url')
    title = db.Column(db.String(64), nullable=True, comment='案件名称')
    case_no = db.Column(db.String(64), nullable=True, comment='行政处罚决定书文号', unique=True)
    criminal_type = db.Column(db.String(1024), nullable=True)
    criminal_content = db.Column(db.Text, nullable=True, comment='违法事实')
    penalty_basis = db.Column(db.String(1024), nullable=True, comment='处罚依据')
    penalty_category = db.Column(db.String(1024), nullable=True, comment='处罚类别')
    penalty_content = db.Column(db.String(1024), nullable=True, comment='处罚内容')
    forfeit = db.Column(db.String(1024), nullable=True, comment='罚款金额(万元)')
    confiscation = db.Column(db.String(1024), nullable=True, comment='没收违法所得、没收非法财物的金额(万元)')
    revoke_cert = db.Column(db.String(1024), nullable=True, comment='暂扣或吊销证照名称及编号')
    judge_date = db.Column(db.Date, nullable=True, comment='处罚决定日期')
    admin_org = db.Column(db.String(1024), nullable=True, comment='处罚机关')
    admin_org_code = db.Column(db.String(1024), nullable=True, comment='处罚机关统一社会信用代码')
    source_org = db.Column(db.String(1024), nullable=True, comment='数据来源单位')
    source_org_code = db.Column(db.String(1024), nullable=True, comment='数据来源单位统一社会信用代码')
    counterpart = db.Column(db.String(1024), nullable=True, comment='行政相对人名称')
    cp_type = db.Column(db.String(1024), nullable=True, comment='行政相对人类别')
    soc_code = db.Column(db.String(64), nullable=True, comment='统一社会信用代码')
    org_code = db.Column(db.String(64), nullable=True, comment='组织机构代码')
    reg_code = db.Column(db.String(64), nullable=True, comment='工商注册号')
    tax_code = db.Column(db.String(64), nullable=True, comment='税务登记号')
    unit_code = db.Column(db.String(64), nullable=True, comment='事业单位证书号')
    org_reg_id = db.Column(db.String(64), nullable=True, comment='社会组织登记证号')
    legal_person = db.Column(db.String(256), nullable=True, comment='法定代表人')
    lp_cert_type = db.Column(db.String(32), nullable=True, comment='法人证件类型')
    lp_cert_code = db.Column(db.String(128), nullable=True, comment='法人证件号')
    cert_type = db.Column(db.String(32), nullable=True, comment='相对人证件类型')
    cert_code = db.Column(db.String(128), nullable=True, comment='相对人证件号码')
    update_date = db.Column(db.Date, nullable=True, comment='数据更新日期')
    content = db.Column(db.Text, nullable=True, comment='正文')
    note = db.Column(db.Text, nullable=True, comment='备注')
    status = db.Column(db.SmallInteger, nullable=True)
    subject_1 = db.Column(db.String(32), nullable=True, comment='一级主题')
    subject_2 = db.Column(db.String(16), nullable=True, comment='二级主题')
    district = db.Column(db.String(32), nullable=True, comment='地域')
    district_level = db.Column(db.String(16), nullable=True, comment='地域层级')
    org_type = db.Column(db.String(32), nullable=True, comment='处罚机关类型')
    counterpart_type = db.Column(db.String(16), nullable=True, comment='相对人类别')
    penalty_org_type = db.Column(db.String(32), nullable=True, comment='被处罚对象类型')
    penalty_person_type = db.Column(db.String(48), nullable=True, comment='被处罚对象类型')